//
// Created by jinling on 2025/6/24.
//
#pragma once
#include <pthread.h>

#include "NonCopyable.h"

namespace tmms
{
    namespace  base
    {
        template<typename  T>
        class Singleton:public  NonCopyable
        {
        public:
            Singleton() = delete;
            ~Singleton() = delete;

            static T*& Instance()//访问函数
            {
                pthread_once(&ponce_,&Singleton::inti); //第一次调用就要初始化，使用pthread_once_t函数，而不用每次都加锁，影响速率
                return value_;
            }
        private:
            static void inti()
            {
                if (!value_)
                {
                    value_ = new T();
                }
            }

            static  pthread_once_t ponce_;
            static  T* value_;
        };

        //静态成员变量的初始化要在类的外面
        template<typename T>
        pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;

        template<typename T>
        T* Singleton<T>::value_ = nullptr;
    }
}